<template>
  <ContentWrap>
    <el-form :model="queryParams" ref="queryFormRef" label-width="auto" :inline="true">
      <el-form-item label="仓库名称" prop="warehouseName">
        <el-input
          v-model="queryParams.warehouseName"
          placeholder="请输入仓库名称"
          clearable
          @keyup.enter="getPage"
        />
      </el-form-item>
      <el-form-item>
        <el-button v-hasPermi="['material:warehouse:query']" @click="handleClickSearch">
          <Icon icon="ep:search" />
          搜索
        </el-button>
        <el-button @click="handleClickReset">
          <Icon icon="ep:refresh" />
          重置
        </el-button>
        <el-button v-hasPermi="['material:warehouse:create']" @click="handleClickCreate">
          <Icon icon="ep:plus" />
          新增
        </el-button>
      </el-form-item>
    </el-form>
    <Table
      :columns="columns"
      :page-param="queryParams"
      adaptive
      height-per="90"
      :page-data="queryData"
      save-key="materialWarehouse"
      @page-change="getPage"
      @row-dblclick="handleTableDblclick"
    >
      <template #menu="{ row }">
        <context-menu-item
          label="修改"
          @click="handleMenuUpdate(row)"
          v-hasPermi="['material:warehouse:update']"
        />
        <context-menu-item
          label="删除"
          @click="handleMenuDelete(row)"
          v-hasPermi="['material:warehouse:delete']"
        />
      </template>
    </Table>
  </ContentWrap>
  <MaterialWarehouseEdit ref="editFormRef" @success="getPage" />
</template>
<script setup name="MaterialWarehouse" lang="ts">
import * as materialWarehouseApi from '@/api/material/warehouse'
import MaterialWarehouseEdit from './MaterialWarehouseEdit.vue'
const editFormRef = ref()
const queryFormRef = ref()
const queryParams = ref({
  warehouseName: undefined
})
const queryData = ref()
const columns = [
  {
    label: '仓库名称',
    prop: 'warehouseName'
  },
  {
    label: '仓库负责人',
    prop: 'warehouseDirector'
  },
  {
    label: '仓库地址',
    prop: 'warehouseAddress'
  },
  {
    label: '备注',
    prop: 'remark'
  },
  {
    label: '创建时间',
    prop: 'createTime'
  }
]

onMounted(() => {
  getPage()
})

const handleClickSearch = () => {
  getPage()
}

const handleClickReset = () => {
  queryFormRef.value?.resetFields()
  handleClickSearch()
}

const handleClickCreate = () => {
  editFormRef.value.open('create', 0)
}

const getPage = async () => {
  try {
    queryData.value = await materialWarehouseApi.pageQuery(queryParams.value)
  } finally {
  }
}

const handleMenuUpdate = (row) => {
  editFormRef.value.open('update', row.id)
}

const handleTableDblclick = (row) => {
  editFormRef.value.open('update', row.id)
}

const handleMenuDelete = (row) => {
  materialWarehouseApi.deleted(row.id)
  getPage()
}
</script>
